package com.globalwave.pos.purchase.web;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.globalwave.base.BaseAction;
import com.globalwave.common.Convertor;
import com.globalwave.common.cache.CodeHelper;
import com.globalwave.customer.entity.CustomerProfile;
import com.globalwave.customer.service.CustomerProfileBO;
import com.globalwave.pos.inventory.entity.ProductItemSO;
import com.globalwave.pos.payment.entity.PaymentLine;
import com.globalwave.pos.purchase.entity.PurchasesOrder;
import com.globalwave.pos.purchase.entity.PurchasesOrderSO;
import com.globalwave.pos.purchase.service.PurchasesOrderBO;
import com.globalwave.system.web.annotations.Pid;
import com.opensymphony.xwork2.Preparable;


@Service("pos_purchasesOrderAction")
@Scope("prototype")
public class PurchasesOrderAction extends BaseAction implements Preparable {

    private static final long serialVersionUID = 8582051849254108462L;
    
    
    private PurchasesOrderBO purchasesOrderBO ;
    private PurchasesOrder purchasesOrder ;
    private PurchasesOrderSO purchasesOrderSO ;
    private ProductItemSO productItemSO ;
    private Integer quantity ;
    private CustomerProfileBO customerProfileBO ;
    private Long cid ;
    

    @Pid(value=1000)
    public String addItem()  throws Exception {              
        
    	PurchasesOrder order = (PurchasesOrder)getSession().getAttribute(PurchasesOrder.SESSION_ID) ;
    	    	
    	boolean isNotExist = (order == null) ;
    	
    	order = purchasesOrderBO.addItem(order, productItemSO) ;
    	
    	if (isNotExist) {
    		getSession().setAttribute(PurchasesOrder.SESSION_ID, order) ;
    	}

        renderXML(this.getOrderMessage(order, "").asXML()) ;
        
        return null;        
    }

    public String getPaymentMethods()  throws Exception {

    	PurchasesOrder order = (PurchasesOrder)getSession().getAttribute(PurchasesOrder.SESSION_ID) ;
    	
        renderXML(
        		this.getPaymentMethods(
        				purchasesOrderBO.getPayementMethods(order), "").asXML()) ;
        
    	return null ;
    }
    
    @Pid(value=1000)
    public String payAndAutoComplete() throws Exception {
        
    	PurchasesOrder order = (PurchasesOrder)getSession().getAttribute(PurchasesOrder.SESSION_ID) ;
    	PurchasesOrder payOrder = new PurchasesOrder() ;
    	payOrder.setPaymentLines((List)Convertor.xml2List(super.getParamsAsXml(), PaymentLine.class)) ;
    	purchasesOrderBO.payAndAutoComplete(order, payOrder) ;

        renderXML(this.getOrderMessage(order, "").asXML()) ;
        
        clear();
        
    	return null ;
    }
    
    public String clear() throws Exception {
    	
    	getSession().removeAttribute(PurchasesOrder.SESSION_ID) ;
    	
    	return null ;
    }

    public Element getOrderMessage(PurchasesOrder order,String msg) throws Exception {

        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        Element root = document.addElement("root") ;
        root.addElement("message").addText(msg) ;
        
        if (order != null) {
        	Element eOrder = Convertor.object2Xml(order) ;
        	if (order.getLines() != null) {
        	    eOrder.add(Convertor.list2Xml(order.getLines(), "lines")) ;
        	}
        	root.addElement("data").add(eOrder);
        }
        
        root.addElement("versionId").addText(String.valueOf(CodeHelper.getVersionId())) ;
        
        return root ;
             
    }

    public Element getPaymentMethods(List<PaymentLine> paymentLines,String msg) throws Exception {

        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        Element root = document.addElement("root") ;
        
        root.addElement("message").addText(msg) ;
        root.add(Convertor.list2Xml(paymentLines, "paymentLines"));
        
        
        return root ;
             
    }

    @Pid(value=1000)
    public String getSubscriber()  throws Exception {              

        this.purchasesOrder = purchasesOrderBO.newOrder() ;
		getSession().setAttribute(PurchasesOrder.SESSION_ID, this.purchasesOrder) ;
		
		CustomerProfile customerProfile = (CustomerProfile)getCustomerProfileBO().get(cid) ;
        this.purchasesOrder.setSubscriber_number(customerProfile.getCid()) ;
        this.purchasesOrder.setSubscriber_type(customerProfile.getType_()) ;

        renderXML(this.getMessage(customerProfile, "").asXML()) ;
        
        return null;    
        
    }

    public String getConsignee()  throws Exception {              

        this.purchasesOrder = (PurchasesOrder) getSession().getAttribute(PurchasesOrder.SESSION_ID) ;
		
		CustomerProfile customerProfile = (CustomerProfile)getCustomerProfileBO().get(cid) ;
        this.purchasesOrder.setConsignee_number(customerProfile.getCid()) ;

        renderXML(this.getMessage(customerProfile, "").asXML()) ;
        
        return null;    
        
    }

    public String query()  throws Exception {
    	
        renderXML(purchasesOrderBO.query(purchasesOrderSO, pageIndex, pageSize, null).asXML()) ;
        
    	return null ;
    }
    

    public String get()  throws Exception {

    	
        renderXML(this.getOrderMessage(purchasesOrderBO.getWithLines(purchasesOrder), "").asXML()) ;
        
    	return null ;
    }

    public String cancel()  throws Exception {
    	
    	purchasesOrderBO.cancel(this.purchasesOrder) ;
    	
    	return null ;
    }
    

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}
    
	public PurchasesOrderBO getPurchasesOrderBO() {
		return purchasesOrderBO;
	}

	public void setPurchasesOrderBO(PurchasesOrderBO purchasesOrderBO) {
		this.purchasesOrderBO = purchasesOrderBO;
	}


	public PurchasesOrder getPurchasesOrder() {
		return purchasesOrder;
	}


	public void setPurchasesOrder(PurchasesOrder purchasesOrder) {
		this.purchasesOrder = purchasesOrder;
	}


	public Integer getQuantity() {
		return quantity;
	}


	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public ProductItemSO getProductItemSO() {
		return productItemSO;
	}

	public void setProductItemSO(ProductItemSO productItemSO) {
		this.productItemSO = productItemSO;
	}

	public CustomerProfileBO getCustomerProfileBO() {
		if (customerProfileBO == null) {
			this.customerProfileBO = 
				(CustomerProfileBO)CodeHelper.getAppContext().getBean("customerProfileBO") ;
		}
		return customerProfileBO;
	}

	public Long getCid() {
		return cid;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	public PurchasesOrderSO getPurchasesOrderSO() {
		return purchasesOrderSO;
	}

	public void setPurchasesOrderSO(PurchasesOrderSO purchasesOrderSO) {
		this.purchasesOrderSO = purchasesOrderSO;
	}




    
}
